package com.tzy.order.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzy.common.entity.Dishes;
import com.tzy.common.entity.Orders;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tzy.common.entity.Shop;
import com.tzy.common.entity.ShoppingCart;
import com.tzy.order.mapper.OrdersMapper;
import com.tzy.order.util.GetNumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author 刘舒
 * @since 2022-03-03
 */
@Service
public class OrdersService extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Resource
    private OrdersMapper ordersMapper;
    private String dindanhao;

    /**
     * 根据id连表查询订单信息
     * @param userId
     * @return
     */
    @Override
    public List<Orders> getListById(int userId) {
        return ordersMapper.findListById(userId);
    }

    @Override
    public Orders selorder(String orderId) {
        Orders orders = ordersMapper.selorder(orderId);
        if(orders!=null){
            return orders;
        }
        return null;
    }

    public Orders getOrder(int orderId){
        return ordersMapper.findOrder(orderId);
    }


    /**
     * 分页查询
     *
     * @param pageNum
     * @param size
     * @return
     */
    public Page<Orders> orderListByPage(Integer pageNum, Integer size,String shopName) {
        if( pageNum!=null && size!=null ){
            if(pageNum <= 0 ){
                pageNum = 0;
            }else{
                pageNum=(pageNum-1)*size;
            }
        }


        List<Orders> list = ordersMapper.findList(pageNum, size,shopName);
        Page<Orders> page = new Page<>();
        page.setRecords(list);
        page.setTotal(ordersMapper.findTotal());
        page.setCurrent(pageNum);
        page.setSize(size);
        return page;
    }

    /**
     * 修改订单
     *
     * @param orders
     * @return
     */
    public int update(Orders orders) {
        return ordersMapper.updateById(orders);
    }

    /**
     * 添加订单
     *
     * @param orders
     * @return
     */
    public String insert(Orders orders) {
        String dindan = GetNumberUtil.getOrderNumber();
        orders.setOrderNo(dindan);
        Integer num = ordersMapper.insert(orders);
        if(num>0){
            return dindan;
        }
        return null;
    }

    /**
     * 根据id删除用户订单
     *
     * @param id
     * @return
     */
    public int removeById(int id) {
        return ordersMapper.deleteById(id);
    }
    /**
     * 批量删除订单信息
     * @param ids
     * @return
     */
    public int deleteOrderByIds(String[] ids){


        return ordersMapper.deleteOrderByIds(ids);

    }


}
